package www.bplead.com.mydemo.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import www.bplead.com.controller.action.user.event.UserInfoPageRequest;
import www.bplead.com.controller.action.user.event.UserInfoPageResponse;
import www.bplead.com.mydemo.R;
import www.bplead.com.mydemo.adapter.RefreshListViewAdapter;
import www.bplead.com.mydemo.widget.RefreshListView;

/**
 * Created by wangyangzi on 2016/9/14.
 */
public class ListViewScrollActivity extends BaseActivity implements RefreshListView.LoadDataListener,View.OnClickListener {
    private static final String TAG = "ListViewScrollActivity";

    RefreshListView listView;
    RefreshListViewAdapter adapter;

    //分页显示，每页30条记录
    int pageSize = 30;
    int startPos = 0;
    int endPos = startPos + (pageSize - 1);

    int load_type;
    //进入页面时首次加载
    static final int load_type_first = 0;
    //下拉刷新加载
    static final int load_type_refresh = 1;
    //滚动到底部加载更多数据
    static final int load_type_more = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_scroll);

        listView = (RefreshListView) this.findViewById(R.id.refresh_list_lv);
        listView.setLoadDataListener(this);

        load_type = load_type_first;
        sendRequest();
    }

    private void sendRequest(){
        UserInfoPageRequest request = new UserInfoPageRequest();
        request.startPos = startPos;
        request.endPos = endPos;
        EventBus.getDefault().post(request);
    }

    @Subscribe
    public void getUserList(UserInfoPageResponse response) {
        Log.v(TAG, "getUserList function : " + response.userInfoList);
        switch (load_type){
            case load_type_first:
                adapter = new RefreshListViewAdapter(this,response.userInfoList);
                listView.setAdapter(adapter);
            case load_type_refresh:
                if(response.userInfoList.size() > 0){
                    adapter.setUserList(response.userInfoList);
                    adapter.notifyDataSetChanged();
                }
                listView.loadLatestDataComplete();
                break;
            case load_type_more:
                if(response.userInfoList.size() > 0){
                    adapter.getUserList().addAll(response.userInfoList);
                    adapter.notifyDataSetChanged();
                }
                listView.loadMoreDataComplete();
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 下拉刷新
     */
    @Override
    public void pullRefreshRequest() {
        startPos = 0;
        endPos = startPos + (pageSize - 1);
        load_type = load_type_refresh;
        sendRequest();
    }

    /**
     * 滚动到底部加载更多
     */
    @Override
    public void loadMoreRequest() {
        startPos = endPos + 1;
        endPos = startPos + (pageSize - 1);
        load_type = load_type_more;
        sendRequest();
    }
}
